import * as server from "@minecraft/server";
import * as data_item from "../data/item";
import * as realm_property from "../system/realm_property";
import * as derived_tools from "../system/derived_tools";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
export function Collection(block, permutation) {
    if (!realm_property.ExpendEnergy(block, -30))
        return;
    const setOptions = {
        maxDistance: permutation.getState('STATE:value') * 8,
        type: "minecraft:item",
        location: block
    };
    const items = block.dimension.getEntities(setOptions);
    const container = block.below()?.getComponent('inventory')?.container;
    items.forEach(info => native_tools.TrySpawnParticle(info.dimension, 'constant:smoke_rune_purple', info.location));
    if (container && container.emptySlotsCount >= items.length)
        items.forEach(info => {
            const item = info.getComponent('item')?.itemStack;
            container.addItem(item);
            info.remove();
        });
    else
        items.forEach(info => info.teleport(block.center()));
}
;
function getTargetBlock(block) {
    const setBlcokGroup = [];
    for (let index = 2; index < 64; index++) {
        const getBlock = block.offset({ x: 0, y: -index, z: 0 });
        if (getBlock)
            setBlcokGroup.push(getBlock);
    }
    ;
    return setBlcokGroup.filter(block => !block.isAir)[0];
}
;
function FillingTest(block, container, location) {
    for (let index = 0; index < container.size; index++) {
        const getItem = container.getItem(index);
        if (!getItem)
            continue;
        try {
            const blockID = data_item.crops_map.get(getItem.typeId) ?? getItem.typeId;
            native_tools.TryFillBlocks(block.dimension, location, location, server.BlockPermutation.resolve(blockID));
            if (getItem.amount > 1) {
                getItem.amount -= 1;
                container.setItem(index, getItem);
            }
            else {
                container.setItem(index);
            }
            ;
            break;
        }
        catch {
            continue;
        }
    }
}
;
export function Placement(block) {
    if (!realm_property.ExpendEnergy(block, -60))
        return;
    const aboveBlock = block.above();
    const getContainer = aboveBlock?.getComponent('minecraft:inventory')?.container;
    if (!getContainer)
        return derived_tools.ErrorMessage('<§l§b 方块放置 §r>§4 发生错误§r', block, { text: '未能在设备上方找到合适的<§l§3 方块容器 §r>' });
    if (getContainer.size > 1) {
        const getTarget = getTargetBlock(block) ?? block;
        const getPlace = math_tools.Vector.add(getTarget, math_tools.Vector.CONSTANT_UP);
        block.dimension.runCommand(`fill ${getPlace.x} ${getPlace.y} ${getPlace.z} ${getPlace.x} ${getPlace.y} ${getPlace.z} air [] destroy`);
        server.system.runTimeout(() => FillingTest(block, getContainer, getPlace), 5);
        achieve_plan.PathExecute.Create('方块放置-检测射线', 1, {
            location_group: [block, getPlace],
            particles: ['constant:track_color_rainbow'],
            offset: { x: 0.5, y: 0.5, z: 0.5 },
            dimension: block.dimension,
            cooldown: 1,
            speed: 1
        });
    }
}
;
export function Transmission(block, type) {
    if (!realm_property.ExpendEnergy(block, -60))
        return;
    const setItemGroup = [];
    function QueryContainer(source) {
        try {
            const getBlockOpen = block.offset(source);
            const getContainerOpen = getBlockOpen?.getComponent('minecraft:inventory')?.container;
            for (let index = 1; index < 64; index++) {
                try {
                    const pointer = math_tools.Vector.scale(source, -index);
                    const getBlockDone = block.offset(pointer);
                    if (!getBlockDone)
                        continue;
                    const getContainerDone = getBlockDone.getComponent('minecraft:inventory')?.container;
                    if (!getContainerOpen || !getContainerDone)
                        continue;
                    let getEmptyCount = getContainerDone.emptySlotsCount;
                    for (let index = 0; index < getContainerOpen.size; index++) {
                        const getItem = getContainerOpen.getItem(index);
                        if (getItem && getEmptyCount != 0) {
                            getContainerOpen.setItem(index);
                            setItemGroup.push(getItem);
                            getEmptyCount--;
                        }
                    }
                    ;
                    for (let item of setItemGroup)
                        getContainerDone.addItem(item);
                    achieve_plan.PathExecute.Create('打包投送-射线动画', 1, {
                        location_group: [block.location, getBlockDone.location],
                        particles: ['constant:track_rune_orange'],
                        offset: math_tools.Vector.CONSTANT_HALF,
                        dimension: block.dimension,
                        cooldown: 1,
                        speed: 1,
                    });
                    break;
                }
                catch {
                    continue;
                }
            }
        }
        catch {
            derived_tools.ErrorMessage('<§l§b 打包投送 §r>§4 发生错误§r', block, { text: '未能在设备后方找到合适的<§l§3 方块容器 §r>' });
        }
    }
    ;
    switch (type) {
        case 'east':
            QueryContainer(math_tools.Vector.CONSTANT_WEST);
            break;
        case 'west':
            QueryContainer(math_tools.Vector.CONSTANT_EAST);
            break;
        case 'south':
            QueryContainer(math_tools.Vector.CONSTANT_NORTH);
            break;
        case 'north':
            QueryContainer(math_tools.Vector.CONSTANT_SOUTH);
            break;
        case 'up':
            QueryContainer(math_tools.Vector.CONSTANT_DOWN);
            break;
        case 'down':
            QueryContainer(math_tools.Vector.CONSTANT_UP);
            break;
    }
    ;
}
;
